package com.whutosa.template.server.controller;

import com.whutosa.template.server.model.User;
import com.whutosa.template.server.model.VO;
import com.whutosa.template.server.service.UserService;
import com.whutosa.template.server.utils.JedisUtil;
import com.whutosa.template.server.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 19110 on 2016/9/28.
 */
@Api
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public VO login(
            @ApiParam("用户id") @RequestParam(name = "uid") String uid,
            @ApiParam("密码") @RequestParam(name = "pswd") String pswd
    ) {
        User user = userService.login(uid, pswd);
        if (user == null) {
            return VO.WRONG_UID_PSWD;
        } else {
            String token = TokenUtil.genToken(user.getUid());
            JedisUtil.set(token, user.getUid());
            return new VO<>(token);
        }
    }

}
